package com.nach0x.theTVDBrenamer;

import com.moviejukebox.thetvdb.TheTVDB;
import com.moviejukebox.thetvdb.model.Episode;
import com.moviejukebox.thetvdb.model.Series;
import java.util.List;

/**
 *
 * @author nacho
 */
public class Search {
    
    TheTVDB tvDB;

    public Search() {
        
        tvDB = new TheTVDB("5B757353E14FA17B");
        
    }
    
    public void getEpisodeInfo(EpisodeAtrb ep) throws Exception {
        
        List<Series> results = tvDB.searchSeries(ep.getSeriesName(), "en");
        
        for(int i = 0; i < results.size(); i++) {
            
            if(results.get(i).getSeriesName().compareToIgnoreCase(ep.getSeriesName()) == 0) {
                
                Series serie = results.get(i);
                
                try {
                    
                    int season = new Integer(ep.getSeasonNbr());
                    int episode = new Integer(ep.getEpisodeNbr());
                    
                    Episode ep2 = tvDB.getEpisode(serie.getId(), season, episode, "en");
                    
                    ep.setEpisodeName(ep2.getEpisodeName());
                    
                    return;
                    
                } catch(Exception e) {
                    
                    throw new Exception(ep.getSeriesName() + ": no season or episode found");
                    //TODO ofrecer una lista de series que puedan estar siendo buscada
                }
            }
        }
        
        throw new Exception(ep.getSeriesName() + ": no series found");
    }
    
}
